package com.app.dao;

import com.app.domain.Products;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductsDao {
    // 插入一条新记录
    @Insert("insert into products(product_id,name,publisher,expire,personal_limit,saled,risk,settlement_type,onsale,description,location,gmt_create,gmt_update,exclusions,type,coverage_amount,premium)" +
            "values(#{product_id},#{name},#{publisher},#{expire},#{personal_limit}, #{saled} ,#{risk},#{settlement_type},#{onsale},#{description},#{location},#{gmt_create},#{gmt_update},#{exclusions},#{type},#{coverage_amount},#{premium})")
    void insertProducts(Products products);

    // 获取全部product
    @Select("select * from products")
    List<Products> getAll();

    // 获取全部已上线的product
    @Select("select * from products where onsale = 1")
    List<Products> getAllOnsale();
    // 根据发布者查询商品全部信息
    @Select("select * from products where publisher = #{publisher}")
    List<Products> getProductsByPublisher(Integer publisher);

    // 根据商品id查询商品全部信息
    @Select("select * from products where product_id=#{product_id}")
    Products getProductsById(Integer product_id);

    // 根据发布者（外键publisher，指向admin_id，是admin表的主键）查询发布者相同的产品
    @Select("select * from products group by publisher")
    List<Products> getPublisherProducts();

    // 根据商品名称进行模糊查询，得到商品全部信息
    @Select("select * from products where name like '%#{name}%'")
    List<Products> queryProductsLikeName(String name);

    //根据商品id查询商品可购买地域
    @Select("select location from products where product_id=#{product_id}")
    String getLocById(Integer product_id);

    // 根据商品id修改商品基础信息，包括商品名称、商品期限、每人限额
    // 售出数量、风险等级、结息方式、是否上线、商品说明、可购买地域、
    // 免责声明、保险类型、保障金额、保费
    @Update("update products set name = #{name} , expire = #{expire} , " +
    "personal_limit = #{personal_limit} ,saled = #{saled} , risk = #{risk} , " +
    "settlement_type = #{settlement_type} , onsale = #{onsale} , saled = #{saled} , " +
    "description = #{description} , location = #{location} ,exclusions=#{exclusions}," +
    "type=#{type}, coverage_amount=#{coverage_amount},premium=#{premium} where product_id = #{product_id}")
    void UpdateProductsInfo(Products products);

    // 根据商品id删除一条记录
    @Delete("delete from products where product_id=#{product_id}")
    void DeleteById(Integer product_id);
}
